package com.pro.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pro.dao.BookMapper;
import com.pro.dao.CategoryMapper;
import com.pro.dao.ShoucangMapper;
import com.pro.domain.Book;
import com.pro.domain.Category;
import com.pro.domain.Member;
import com.pro.domain.Shoucang;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Service
//@Transactional(propagation = Propagation.REQUIRED)
@Transactional(propagation = Propagation.NOT_SUPPORTED,readOnly = true)
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private ShoucangMapper shoucangMapper;
    @Autowired
    private ShoucangService shoucangService;

    @Override
    public List<Category> selectAll() {
        List<Category> categoryList = categoryMapper.selectList(new QueryWrapper<Category>());

        return categoryList;
    }

    @Override
    public List<Book> selectOne() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("category_id",6);
        List<Book> bookList = bookMapper.selectList(queryWrapper);
        return bookList;
    }

    @Override
    public List<Book> selectMysave(HttpSession session) {
        Member member= (Member) session.getAttribute("loginMember");
        Long memberId = member.getMemberId();/*拿到member_id*/
        List<Shoucang> shoucangList=shoucangService.getBookIdByMemberId(memberId);/*根据member_id查到shoucangList*/
        List<Long> bookIdList = new ArrayList<Long>();
        for (int i = 0; i < shoucangList.size(); i++) {
            Long bookId = shoucangList.get(i).getBookId();
            bookIdList.add(bookId);
        }/*将所有bookId存到一个集合里*/

        List<Book> bookList = new ArrayList<Book>();
        for (int i = 0; i < bookIdList.size(); i++) {
            QueryWrapper<Book> queryWrapper = new QueryWrapper<Book>();
            queryWrapper.eq("book_id",bookIdList.get(i));
            Book books =  bookMapper.selectOne(queryWrapper);
            bookList.add(books);
        }
        return bookList;
    }
}
